package com.hsxshop.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hsxshop.model.Pagination;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangchongya on 17/6/28.
 */
public class JsonTool {

    /**
     * 返回list json
     * @param list
     * @return
     */
    public static String returnListJsonSuccess(List list, Pagination pagination){
        ObjectMapper mapper = new ObjectMapper();
        Map map=new HashMap();
        map.put("list",list);
        map.put("state","success");
        map.put("message","获取成功");
        map.put("refresh",true);
        map.put("code",200);
        Map page=new HashMap();
        page.put("pageIndex",pagination.getPageIndex());
        page.put("pageSize",pagination.getPageSize());
        page.put("allRows",pagination.getAllRows());
        page.put("pageCounts",pagination.getPageCounts());
        map.put("page",page);
        String result="";
        try {
            result = mapper.writeValueAsString(map);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 返回object json
     * @param obj
     * @return
     */
    public static String returnObjectJsonSuccess(Object obj){
        ObjectMapper mapper = new ObjectMapper();
        Map map=new HashMap();
        map.put("data",obj);
        map.put("state","success");
        map.put("message","操作成功");
        map.put("refresh",true);
        map.put("code",200);
        String result="";
        try {
            result = mapper.writeValueAsString(map);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 返回object json
     * @param obj
     * @return
     */
    public static String returnObjectJsonError(Object obj){
        ObjectMapper mapper = new ObjectMapper();
        Map map=new HashMap();
        map.put("data",obj);
        map.put("state","false");
        map.put("message","操作失败");
        map.put("refresh",false);
        map.put("code",201);
        String result="";
        try {
            result = mapper.writeValueAsString(map);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 返回object json
     * @param msg
     * @return
     */
    public static String returnObjectJsonErrorMsg(String msg){
        ObjectMapper mapper = new ObjectMapper();
        Map map=new HashMap();
        map.put("state","false");
        map.put("message",msg);
        map.put("refresh",false);
        map.put("code",201);
        String result="";
        try {
            result = mapper.writeValueAsString(map);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 返回object json
     * @return
     */
    public static String return401Error(){
        ObjectMapper mapper = new ObjectMapper();
        Map map=new HashMap();
        map.put("state","false");
        map.put("message","未登录");
        map.put("url","/html/index.html");
        map.put("code",401);
        String result="";
        try {
            result = mapper.writeValueAsString(map);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
